// pages/to-evaluate/to-evaluate.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userimgsrc: '../../images/head01.png',
    imageList: [],
    uploadimgsrc:'../../images/xiangji.png',
    iconac:1,
    order_id:'',
    id:'',

  },

  //选取
  imgupload:function(){
    var that=this;
    // wx.chooseImage({
    //   count:6, // 默认9
    //   sizeType: ['original', 'compressed'],
    //   sourceType: ['album', 'camera'],
    //   success: function (res){
    //     console.log('1111111',res)
    //     that.setData({
    //       imageList: res.tempFilePaths
    //     })
    //   }
    // })
    wx.chooseImage({
      count: 3, // 默认9
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        that.setData({
          imageList: res.tempFilePaths
        })
        var tempFilePaths = res.tempFilePaths
        var a = [];
        for (i = 0; i < tempFilePaths.length;i++){
          var i = i;
          wx.uploadFile({
            url: app.globalData.directory + 'api/v2/comment/upload', //仅为示例，非真实的接口地址
            filePath: tempFilePaths[i],
            header: {
              'content-type': 'application/json', // 默认值
              'token': wx.getStorageSync('token')
            },
            name: 'file',
            success: function (res) {
              var data = res.data
              var reg = new RegExp('"', "g");  
              data = data.replace(reg, "");
              // console.log('22222222222', data)
              a.push(data)
              //do something
              that.setData({
                id: a.toString()
              })
              console.log(that.data.id)
            },
             fail: function (res) {
              console.log(11111111)
              console.log(res)
            }
          })
        }
      }
      
    });
  },
  //预览
  previewimage:function(e){
    var current = e.target.dataset.src;
    wx.previewImage({
      current: current,
      urls: this.data.imageList
    })
  },
  //点击更换
  iconchange:function(e){
    console.log(e.detail.value);
    if (this.data.iconac=='0'){
      this.setData({
        iconac:1
      })
    }else{
      this.setData({
        iconac:0
      })
    }
  },
  formSubmit:function(e){
    let formData = e.detail.value.content;
    console.log(formData)
    var order_id = this.data.order_id;
    var id = this.data.id;
    var is_hide = this.data.iconac;
    console.log('--------',id)
    console.log('--------', order_id)
    console.log('-----------', is_hide)
    wx.request({
      url: app.globalData.directory + 'api/v2/comment/add',
      method: 'POST',
      data: {
        content:formData,
        order_id:order_id,
        image_id:id,
        is_hide:is_hide
      },
      header: {
        'content-type': 'application/json', // 默认值
        'token': wx.getStorageSync('token')
      },
      success: function (res) {
        // console.log('88888888888888888888888',res.data)
        if(res.data.status == 'ok'){
          wx.showToast({
            title: res.data.msg,
            icon: 'success',
            duration: 2000
          })
          setTimeout(function () {
            wx.switchTab({
              url: '../index/index',
            })
          }, 2000)
        }else{
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000
          })
        }
        // if (res.data.msg == "ok") {

          


        // }

      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      order_id:options.id
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})